package operation;

import com.supermap.data.Dataset;
import com.supermap.data.Datasource;
import compareResult.datasetCompare.DatasetComparer;
import org.junit.rules.ErrorCollector;

/**
 * Created by hanyz on 2017/11/2.
 */
public class CompareOperation {
	private ErrorCollector errorCollector;
	private static CompareOperation compareOperation;

	private CompareOperation() {
	}

	public static CompareOperation getInstance(ErrorCollector Collector) {
		if (compareOperation == null) {
			compareOperation = new CompareOperation();
		}
		compareOperation.errorCollector = Collector;
		return compareOperation;
	}

	public static CompareOperation getInstance() {
		if (compareOperation == null) {
			compareOperation = new CompareOperation();
		}
		return compareOperation;
	}

	/**
	 * 比较指定数据源下的一个数据集
	 *
	 * @param expDatasoure
	 * @param actualDatasource
	 * @param expDatasetName
	 * @param actualDatasetName
	 * @return
	 * @throws Exception
	 */
	public boolean compareDataset(Datasource expDatasoure, Datasource actualDatasource, String expDatasetName, String actualDatasetName) throws Exception {
		if (expDatasoure == null || actualDatasource == null) {
			if (errorCollector != null) {
				errorCollector.addError(new Exception("null Datasoure:" + expDatasoure == null ? expDatasoure.getAlias() : actualDatasource.getAlias()));
			} else {
				throw new Exception("null Datasoure:" + expDatasoure == null ? expDatasoure.getAlias() : actualDatasource.getAlias());
			}
		}
		try {
			Dataset expDataset = expDatasoure.getDatasets().get(expDatasetName);
			Dataset actualDataset = actualDatasource.getDatasets().get(actualDatasetName);
			return new DatasetComparer().compare(expDataset, actualDataset);
		} catch (Exception e) {
			if (errorCollector != null) {
				errorCollector.addError(e);
			} else {
				throw new Exception(e);
			}
		}
		return true;
	}

	/**
	 * 比较指定数据源下的一个数据集,不同数据源下比较的数据集同名
	 *
	 * @param expDatasoure
	 * @param actualDatasource
	 * @param expDatasetName
	 * @return
	 * @throws Exception
	 */
	public boolean compareDataset(Datasource expDatasoure, Datasource actualDatasource, String expDatasetName) throws Exception {
		return compareDataset(expDatasoure, actualDatasource, expDatasetName, expDatasetName);
	}

	/**
	 * 比较指定数据源下的一组数据集,不同数据源下比较的数据集同名
	 *
	 * @param expDatasoure
	 * @param actualDatasource
	 * @param expDatasetNames
	 * @return
	 * @throws Exception
	 */
	public boolean compareDataset(Datasource expDatasoure, Datasource actualDatasource, String[] expDatasetNames) throws Exception {
		for (String expDatasetName : expDatasetNames) {
			compareDataset(expDatasoure, actualDatasource, expDatasetName);
		}
		return true;
	}

	/**
	 * 比较指定数据源下的一组数据集,不同数据源下比较的数据集同名完全相同
	 *
	 * @param expDatasoure
	 * @param actualDatasource
	 * @return
	 * @throws Exception
	 */
	public boolean compareDataset(Datasource expDatasoure, Datasource actualDatasource) throws Exception {
		boolean compareResult = true;
		for (int i = 0; i < actualDatasource.getDatasets().getCount(); i++) {
			compareResult = compareDataset(expDatasoure, actualDatasource, actualDatasource.getDatasets().get(i).getName());
			if (compareResult == false) {
				return false;
			}
		}
		return compareResult;
	}

	/**
	 * 比较指定数据源下的数据集
	 *
	 * @param expDatasetName
	 * @param actualDatasetName Author:caolp
	 */
	public boolean compareDataset(String dataSource, String expDatasetName, String actualDatasetName) throws Exception {
		Dataset expDataset = null;
		Dataset actualDataset = null;
		try {
			Datasource datasource = CommonOperation.getDatasource(dataSource);
			expDataset = datasource.getDatasets().get(expDatasetName);
			actualDataset = datasource.getDatasets().get(actualDatasetName);
		} catch (Exception e) {
			if (errorCollector != null) {
				errorCollector.addError(e);
			} else {
				throw new Exception(e);
			}
		}
		return new DatasetComparer().compare(expDataset, actualDataset);
	}
}
